package com.wnhz.work.jy.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;

import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.MyUtils;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * --忘记密码
 */
public class ForgetActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "ForgetActivity";
    private static final int FORGET_GETCODE = 945;
    private TextView title;
    private EditText forget_phone_num;
    private EditText forget_code;
    private EditText forget_pwd_new;
    private EditText forget_pwd_again;

    private TextView forget_getCode;

    private String phoneNumber;
    private String code;
    private String pwd_one;
    private String pwd_two;

    private int time = 0;

    private String oldphone;
    private String codetime;
    private String first_code;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case FORGET_GETCODE:
                    if (time == 0) {
                        forget_code.setFocusable(true);
                        forget_getCode.setText("获取验证码");
                    } else {
                        time--;
                        forget_getCode.setText("获取验证码" + time);
                        handler.sendEmptyMessageDelayed(FORGET_GETCODE, 1000);
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);

        initView();
    }

    private void initView() {
        forget_phone_num = (EditText) findViewById(R.id.forget_phone_num);
        forget_code = (EditText) findViewById(R.id.forget_code);
        forget_pwd_new = (EditText) findViewById(R.id.forget_pwd_new);
        forget_pwd_again = (EditText) findViewById(R.id.forget_pwd_again);
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("忘记密码");
        forget_getCode = (TextView) findViewById(R.id.forget_getCode);
        forget_getCode.setOnClickListener(this);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.forget_submit).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            case R.id.forget_getCode:
                phoneNumber = forget_phone_num.getText().toString();
                if (MyUtils.isMobileNO(phoneNumber)) {
                    getCode();
                } else {
                    MyToast("请输入正确手机号");
                }

                break;
            case R.id.forget_submit:
                closrKeyboard();
                pwd_one = forget_pwd_new.getText().toString();
                pwd_two = forget_pwd_again.getText().toString();
                phoneNumber = forget_phone_num.getText().toString();
                code = forget_code.getText().toString();
                if (!MyUtils.isMobileNO(phoneNumber)) {
                    MyToast("请输入正确手机号");
                } else if (code == null || "".equals(code)) {
                    MyToast("请输入验证码");
                } else if (!code.equals(first_code)) {
                    MyToast("验证码错误");
                } else if (pwd_one.length() < 6) {
                    MyToast("密码不能少于6位");
                } else if (!phoneNumber.equals(oldphone)) {
                    MyToast("获取验证码的手机号与输入手机号不同");
                } else if (pwd_one == null || "".equals(pwd_one)) {
                    MyToast("请输入新密码");
                } else if (pwd_two == null || "".equals(pwd_two)) {
                    MyToast("请输入新密码");
                } else if (!pwd_one.equals(pwd_two)) {
                    MyToast("两次输入的密码并不相同");
                } else {
                    forget_password();
                }

                break;
            default:
                break;
        }
    }

    private void forget_password() {
        Map<String, Object> map = new HashMap<>();
        map.put("phoneNumber", phoneNumber);
        map.put("oldcode", first_code);
        map.put("code", code);
        map.put("password", pwd_one);
        showDialog();
        XUtil.Post(ConfigUtil.FORGET_PASSWORD, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: " + result);
                try {
                    JSONObject json = new JSONObject(result);
                    String re = json.optString("result");
                    String info = json.optString("info");
                    MyToast(info);
                    if ("1".equals(re)) {
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }

    private void getCode() {
        phoneNumber = forget_phone_num.getText().toString();

        Map<String, Object> map = new HashMap<>();
        map.put("phoneNumber", phoneNumber);
        XUtil.Post(ConfigUtil.GETPAYYZM, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess: code  " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String re = jsonObject.optString("result");
                    if ("1".equals(re)) {
                        JSONObject jsonObject1 = jsonObject.optJSONObject("info");
                        oldphone = jsonObject1.optString("phone");
                        codetime = jsonObject1.optString("time");
                        first_code = jsonObject1.optString("code");
                        time = 60;
                        handler.sendEmptyMessage(FORGET_GETCODE);
                    } else {
                        String info = jsonObject.optString("info");
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                forget_code.setFocusable(true);
            }
        });
    }
}
